@import org.silkframework.workspace.activity.ProjectExecutor
@import plugins.WorkbenchPlugins
@import org.silkframework.workspace.Project
@import plugins.WorkbenchPlugin.TaskActions
@import org.silkframework.runtime.plugin.PluginRegistry
@import controllers.workspace.routes.Assets
@import org.silkframework.config.TaskSpec
@import org.silkframework.workspace.Workspace
@import org.silkframework.workspace.ProjectTask

@(workspace: Workspace)

<script type="application/javascript" >
  $(function () {
    $('#workspace_root').jstree({
      "state": { "key" : "workspace_state" },
      "plugins": [ "state" ]
    });
  });
</script>

<div id="workspace_root">
  <ul  class="filetree">
    @for(p <- workspace.projects) {
      @project(p)
    }
  </ul>
</div>

@project(project: Project) = {
  <li id="project_@project.name" data-jstree='{"icon":"@Assets.at(projectIcon(project))"}' title="@project.loadingErrors.mkString(", ")" >
    @project.name
    @button("Prefixes", "Edit prefixes", "wrench-screwdriver.png", s"editPrefixes('${project.name}')")
    @button("Resources", "Manage resources", "wrench.png", s"editResources('${project.name}')")
    @for(plugin <- WorkbenchPlugins();
         taskActions <- plugin.tasks;
         path <- taskActions.createDialog(project.name)) {
      @button(taskActions.name.takeWhile(_ != ' '), "Add " + taskActions.name, "plus.png", s"workspaceDialog('$path')")
    }
    @button("Link Spec", "Import link specification", "document--plus.png", s"importLinkSpec('${project.name}')")
    @button("Export", "Export project", "document-export.png", s"exportProject('${project.name}')")
    @if(PluginRegistry.availablePlugins[ProjectExecutor].nonEmpty) {
      @button("Execute", "Execute project", "document-export.png", s"executeProject('${project.name}')")
    }
    @button("Clone", "Clone project", "clone.png", s"cloneProject('${project.name}')")
    @button("Remove", "Remove project", "cross.png", s"deleteProjectConfirm('${project.name}')")
    <ul>
      @for(plugin <- WorkbenchPlugins();
           taskActions <- plugin.tasks;
           t <- taskActions.projectTasks(project)) {
        @task(project, t, taskActions)
      }
    </ul>
  </li>
}

@projectIcon(project: Project) = @{
  if(project.loadingErrors.isEmpty)
    "img/project.png"
  else
   "img/project-error.png"
}

@task(project: Project, task: ProjectTask[_ <: TaskSpec], taskActions: TaskActions[_ <: TaskSpec]) = {
  <li id="task_@{project.name}_@task.id" data-jstree='{"icon":"@taskActions.icon"}'>
    @task.id
    @for(path <- taskActions.propertiesDialog(project.name, task.id)) {
      @button("Properties", "Edit task properties", "wrench.png", s"workspaceDialog('$path')")
    }
    @for(path <- taskActions.open(project.name, task.id)) {
      @button("Open", "Open task", "document--pencil.png", s"window.location = '${config.baseUrl}/$path'")
    }
    @button("Clone", "Clone task", "clone.png", s"cloneTask('${project.name}', '${task.id}')")
    @button("Remove", "Remove task", "cross.png", s"deleteTaskConfirm('${project.name}', '${task.id}')")
    <ul>
      @for((key, value) <- taskActions.properties(task.data) if !value.isEmpty) {
        <li data-jstree='{"icon":"@Assets.at("img/file-tag.png")"}'>
          @key: @value
        </li>
      }
    </ul>
  </li>
}

@button(title: String, tooltip: String, icon: String, onClick: String) = {
  <div class="workspace_button" title="@tooltip" onclick="@onClick">
    <img width="16" height="16" src="@Assets.at("img/" + icon)"/>
    <span>@title</span>
  </div>
}